package com.fyzk.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.popupwindow.POKeyBoardPopupwindow;
import com.tyjc.lottery.R;

import static java.lang.Integer.parseInt;

/**
 * 增加 和 减少View
 *
 * @author fxs
 */
public class POASView extends LinearLayout implements OnClickListener, POKeyBoardPopupwindow.OnKeyBoardListener {

    private static final String TAG = "Addition_and_subtractionView";

    private Context context;

    private TextView tv_right;
    private TextView tv_left;
    private Button iv_right;
    private Button iv_left;
    private TextView txt_value;

    private int max = 999999;
    private int min = 1;
    private int leftColor = 0;
    private int rightColor = 0;
    private String tiShi = "";
    private String leftTxt, rightTxt;
    private int num = 1;
    private int unit = 1;

    private String title;

    private POKeyBoardPopupwindow keyBoardPopupwindow;

    public POASView(Context context, AttributeSet attrs,
                    int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        initView(attrs);
    }


    public void setUnit(int unit){
        this.unit = unit;
    }

    public int getUnit() {
        return unit;
    }

    public POASView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView(attrs);
    }

    public POASView(Context context) {
        super(context);
        this.context = context;
        initView(null);
    }

    /**
     * 初始化View
     */
    private void initView(AttributeSet attrs) {
        if (null != attrs) {
            TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.ASView);
            title = type.getString(R.styleable.ASView_remindTitle);
            min = type.getInt(R.styleable.ASView_minNum, 1);
            leftTxt = type.getString(R.styleable.ASView_leftTxt);
            rightTxt = type.getString(R.styleable.ASView_rightTxt);
            leftColor = type.getColor(R.styleable.ASView_leftTxtColor, getResources().getColor(R
                    .color.black));
            rightColor = type.getColor(R.styleable.ASView_rightTxtColor, getResources().getColor
                    (R.color.black));
        }
        View.inflate(context, R.layout.view_addsub_po, this);

        tv_right = (TextView) findViewById(R.id.dj_edit_tv_right);
        tv_left = (TextView) findViewById(R.id.dj_edit_tv_left);
        tv_right.setTextColor(rightColor);
        tv_left.setTextColor(leftColor);
        iv_right =  findViewById(R.id.dj_edit_iv_right);
        iv_left = findViewById(R.id.dj_edit_iv_left);
        txt_value =  findViewById(R.id.dj_edit_et);

        tv_left.setText(leftTxt);
        tv_right.setText(rightTxt);

        txt_value.setFocusableInTouchMode(true);
        txt_value.setOnTouchListener(contentTouchListener);
        iv_left.setOnClickListener(this);
        iv_right.setOnClickListener(this);

    }

    public void setDesVisble(int visble){
        tv_left.setVisibility(visble);
        tv_right.setVisibility(visble);
    }

    public void setASBg(int drawable_id){
        findViewById(R.id.fl_as).setBackgroundResource(drawable_id);
    }

    //==============	右侧信息	==============
    public void setRightText(String text) {
        tv_right.setText(text);
    }

    public void setRightText(int text) {
        tv_right.setText(text);
    }


    //==============	左侧信息	==============
    public void setLeftText(String text) {
        tv_left.setText(text);
    }

    public void setLeftText(int text) {
        tv_left.setText(text);
    }


    //=============		最大值	==============
    public void setMax(int max) {
        this.max = max;
    }

    public void setMax(String max) {
        try {
            this.max = parseInt(max);
        } catch (Exception e) {
            this.max = 99;
        }
    }

    //=============		最小值	==============
    public void setMin(int min) {
        this.min = min;
        this.txt_value.setHint(min + "");
        this.num = min;
    }

    public void setMin(String min) {
        int m;
        try {
            m = parseInt(min);
        } catch (Exception e) {
            m = 0;
        }
        setMin(m);
    }

    public void setValueColor(int color) {
        this.txt_value.setTextColor(ContextCompat.getColor(context, color));
    }

    public void setTiShi(String tiShi) {
        this.tiShi = tiShi;
    }


    private OnTouchListener contentTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (keyBoardPopupwindow == null) {
                keyBoardPopupwindow = new POKeyBoardPopupwindow(getContext(), max, tv_left.getText().toString(),tv_right.getText().toString(),unit);
                keyBoardPopupwindow.setMinMultiple(min);
                keyBoardPopupwindow.setOnKeyBoardListener(POASView.this);
            }
            keyBoardPopupwindow.showPopupWindow(POASView.this, num+"");
            return false;
        }

    };

    public void setTitle(String title) {
        this.title = title;
    }

    AlertDialog contentDialog = null;
    AppCompatEditText editText = null;


//    private void showContentChangeDialog() {
//        if (contentDialog == null) {
//            View view = View.inflate(context, R.layout.content_change, null);
//            editText = (AppCompatEditText) view.findViewById(R.id.edit_content);
//            contentDialog = new AlertDialog.Builder(context).setTitle(title).setView(view)
//                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            dialog.cancel();
//                        }
//                    }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            if (!TextUtil.isEmpty(editText.getText().toString())) {
//                                if (parseInt(editText.getText().toString()) > max) {
//                                    txt_value.setText(max + "");
//                                    AppToast.getToast().show("不能超过" + max);
//                                } else if (parseInt(editText.getText().toString()) < min) {
//                                    AppToast.getToast().show("不能低于" + min);
//                                    txt_value.setText(min + "");
//                                } else {
//                                    txt_value.setText(editText.getText());
//                                }
//                                if (onValueChangedLisener != null) {
//                                    int value = parseInt(txt_value.getText().toString());
//                                    num = value;
//                                    onValueChangedLisener.onValueChanged(POASView.this, value);
//                                }
//
//                            }
//                        }
//                    }).create();
//        }
//        String value = getValue() + "";
//        editText.setText(value);
//        editText.setSelection(value.length());
//        contentDialog.setOnShowListener(new DialogInterface.OnShowListener() {
//            @Override
//            public void onShow(DialogInterface dialog) {
//                editText.setFocusable(true);
//                Utils.toggleSoftwareboard();
//            }
//        });
//        contentDialog.show();
//
//
//    }


    private OnValueChangedLisener onValueChangedLisener = null;

    public void setOnValueChangedLisener(OnValueChangedLisener onValueChangedLisener) {
        this.onValueChangedLisener = onValueChangedLisener;
    }

    public interface OnValueChangedLisener {
        void onValueChanged(POASView view, int value);
    }

    boolean changed = false;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dj_edit_iv_left:
                if (num > min) {
                    changed = true;
                    num-=unit;
                } else {
                    AppToast.getToast().show("不能低于" + min);
                    changed = false;
                }
                break;
            case R.id.dj_edit_iv_right:
                if (num < max) {
                    changed = true;
                    num+=unit;
                } else {
                    AppToast.getToast().show("不能超过" + max);
                    changed = false;
                }
                break;
        }

        txt_value.setText(num + "");
        if (onValueChangedLisener != null) {
            if (changed) {
                onValueChangedLisener.onValueChanged(POASView.this, num);
            }
        }

    }

    public void setValue(int value) {
        num = value;
        txt_value.setText(num + "");
    }


    public void setValue(String text) {
        try {
            num = parseInt(text);
        } catch (Exception e) {
            // TODO: handle exception
            num = 1;
        }
        setValue(num);
    }

    public int getValue() {
        if (num > max) {
            num = max;
        }
        return num;
    }

    public int getText() {
        if (txt_value.getText() == null) {
            num = min;
        }
        return num == min ? min : num;
    }

    @Override
    public void onItem(int position) {

    }

    @Override
    public void popupShow() {

    }

    @Override
    public void popupDismiss(int multiple) {
        num = multiple;
        txt_value.setText(""+multiple);
        if (onValueChangedLisener != null) {
            onValueChangedLisener.onValueChanged(POASView.this, num);
        }
    }
}
